package com.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * Created by hc on 2017/5/22.
 */
@Aspect
@Component
@Slf4j
public class ControllerInterceptor {

    @Pointcut("execution(* com.controller..* (..)) and @annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void controllerMethodPointcut(){}

    @Around("controllerMethodPointcut()")
    public Object Interceptor(ProceedingJoinPoint joinPoint) throws Throwable {
        long beginTime = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        Method method = signature.getMethod();
        String methodName = method.getName();
        log.info("methodName:{}", methodName);


        Object[] args = joinPoint.getArgs();

        for (Object arg: args){
            if (arg instanceof String){
                log.info("字符串为{}",arg);
            }
        }

        Object o = joinPoint.proceed();
        log.info("执行结束, 耗时:{}", System.currentTimeMillis() - beginTime);
        return o;
    }


}
